#include "TutorialApplication.h"
#include <time.h>
#include "enemies/testactor.h"
#include "weapon/wlaser.h"
#include "weapon/wgun.h"
#include "wii.h"

//-------------------------------------------------------------------------------------
TutorialApplication::TutorialApplication(void){
    gestStep = 0;
    m_rumbleTime = 0;
    Wii::wiiSetRumble(false);

    Ogre::Vector2 pointOne;
    pointOne.x = -100;
    pointOne.y = -100;

    Ogre::Vector2 pointTwo;
    pointTwo.x = 500;
    pointTwo.y = 500;

    flamerSet = false;
    flamerCreationTime = m_world->getCurrentTime();
}

//-------------------------------------------------------------------------------------
TutorialApplication::~TutorialApplication(void){}

void TutorialApplication::doFrameStuff(const Ogre::FrameEvent& evt){

    //handle keyboard input with OIS
    mKeyboard->capture();
        if (mKeyboard->isKeyDown(OIS::KC_UP)){
            m_world->getPlayer()->moveUp(evt.timeSinceLastFrame);
        }
        if (mKeyboard->isKeyDown(OIS::KC_DOWN)){
          m_world->getPlayer()->moveDown(evt.timeSinceLastFrame);
        }
        if (mKeyboard->isKeyDown(OIS::KC_LEFT)){
          m_world->getPlayer()->moveLeft(evt.timeSinceLastFrame);
        }
        if (mKeyboard->isKeyDown(OIS::KC_RIGHT)){
          m_world->getPlayer()->moveRight(evt.timeSinceLastFrame);
        }
        if (mKeyboard->isKeyDown(OIS::KC_SPACE)){
            m_world->getPlayer()->fire();
        }
        if (mKeyboard->isKeyDown(OIS::KC_1)){
            m_world->getPlayer()->setWeapon(SPREADER);
        }
        if (mKeyboard->isKeyDown(OIS::KC_2)){
            m_world->getPlayer()->setWeapon(FLAMER);
        }
        if (mKeyboard->isKeyDown(OIS::KC_3)){
            m_world->getPlayer()->setWeapon(LINER);
        }
        if (mKeyboard->isKeyDown(OIS::KC_4)){
             m_world->getPlayer()->setWeapon(FLAMER);
             flamerCreationTime = m_world->getCurrentTime();
             flamerSet = true;
        }
        if (mKeyboard->isKeyDown(OIS::KC_5)){
            m_world->getPlayer()->m_deflecting = true;
            shieldSet = true;
            shieldCreationTime = m_world->getCurrentTime();
            // create shield (set shield state to true)
            // create shield mesh
            // after a certain amount of destroy shield and shield mesh
        }
        if (mKeyboard->isKeyDown(OIS::KC_H)){
            m_world->getPlayer()->setROF(1);
        }
        if (mKeyboard->isKeyDown(OIS::KC_L)){
            m_world->getPlayer()->setROF(2);
        }

    //handle Wiimote input
        if(Wii::isButtonDown(B) || Wii::axisValue(Z) >= 200){
            m_world->getPlayer()->fire();;
        }
        if(Wii::isButtonDown(UP)){
             m_world->getPlayer()->moveUp(evt.timeSinceLastFrame);
        }
        if(Wii::isButtonDown(DOWN)){
             m_world->getPlayer()->moveDown(evt.timeSinceLastFrame);
        }
        if(Wii::isButtonDown(LEFT)){
             m_world->getPlayer()->moveLeft(evt.timeSinceLastFrame);
        }
        if(Wii::isButtonDown(RIGHT)){
             m_world->getPlayer()->moveRight(evt.timeSinceLastFrame);
        }
        if(Wii::getGesture(CCR)){
             m_world->getPlayer()->setWeapon(SPREADER);
             m_world->getPlayer()->fire();
             Wii::setGesture(CCR, false);
             m_world->getPlayer()->setWeapon(LINER);
        }
        if(Wii::getGesture(CR) && !shieldSet){
            Wii::setGesture(CR,false);
            m_world->getPlayer()->m_deflecting = true;
            shieldSet = true;
            m_world->getPlayer()->shield(true);
            shieldCreationTime = m_world->getCurrentTime();
        }
        if(Wii::getGesture(DU)){
            Wii::setGesture(DU,false);
            m_world->getPlayer()->setWeapon(FLAMER);
            flamerCreationTime = m_world->getCurrentTime();
            flamerSet = true;
        }

        if(Wii::isButtonDown(PLUS) || Wii::getGesture(DU)){
            m_world->getPlayer()->weaponNext();
            Wii::setGesture(DU,false);
        }
        if(Wii::isButtonDown(MINUS)){
             m_world->getPlayer()->weaponPrevious();
        }

        // activate rumble for a certain amount of time
        if(Wii::wiiGetRumble()){
            Wii::wiiRumble(true);
            Wii::wiiSetRumble(false);
            m_rumbleTime = m_world->getCurrentTime();
        }

        if(m_world->getCurrentTime() - m_rumbleTime > 200){
            Wii::wiiRumble(false);
            Wii::wiiSetRumble(false);
        }

        if(flamerSet){
            float flamerElapsed = m_world->getCurrentTime() -flamerCreationTime;
            m_world->getPlayer()->fire();
            if(flamerElapsed >= 1000){
                flamerSet = false;
                m_world->getPlayer()->setWeapon(LINER);
            }

        }

        if(shieldSet){
            float shieldElapsed = m_world->getCurrentTime() -shieldCreationTime;
            if(shieldElapsed >= 8000){
                shieldSet = false;
                m_world->getPlayer()->m_deflecting = false;
                m_world->getPlayer()->shield(false);
                //destroy shield
            }

        }

    m_world->worldStep(evt.timeSinceLastFrame);
}

void TutorialApplication::keyPressedEvent(const OIS::KeyEvent &arg ){}

//-------------------------------------------------------------------------------------
void TutorialApplication::createScene(void){

    m_world = new World(mSceneMgr);

    // create plane
    Ogre::Plane universe(Ogre::Vector3::UNIT_Y, -200);
    Ogre::MeshManager::getSingleton().createPlane("ground", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
            universe, 4000, 4000, 20, 20, true, 1, 5, 5, Ogre::Vector3::UNIT_Z);
    Ogre::Entity* entGround = mSceneMgr->createEntity("GroundEntity", "ground");
        mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(entGround);

    entGround->setMaterialName("Ogre/Space");
    entGround->setCastShadows(false);

    // set ambient light
    mSceneMgr->setAmbientLight(Ogre::ColourValue(1, 1, 1));

    //create a light
    Ogre::Light* l = mSceneMgr->createLight("PrimaryLight");
    l->setType(Ogre::Light::LT_POINT);
    l->setDiffuseColour(1,1,1);
    l->setPosition(-300,2000,-1000);

    // create second light
    Ogre::Light* l2 = mSceneMgr->createLight("SecondLight");
    l2->setType(Ogre::Light::LT_POINT);
    l2->setDiffuseColour(1,1,1);
    l2->setPosition(300,2000,-1000);

    srand ( time(NULL) );
}

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
    int main(int argc, char *argv[])
#endif
    {
        // Create application object
        TutorialApplication app;

        try {
            app.go();
        } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
            MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
            std::cerr << "An exception has occured: " <<
                e.getFullDescription().c_str() << std::endl;
#endif
        }

        return 0;
    }

#ifdef __cplusplus
}
#endif
